<template>

  <el-form ref="form" :rules="rules" style="padding-top: 10px;" :model="form" label-width="100px">
    <div class="com-form-title">基础信息</div>
    <el-row>
      <el-col :span="12">
        <el-row>
          <el-col :span="12">
            <el-form-item label="处置单号" prop="number">
              <el-input v-model="form.number" :disabled="disableForm" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="处置金额" prop="amount">
              <el-input v-model="form.amount" :disabled="disableForm" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="处置时间" prop="handleTime">
              <el-date-picker v-model="form.handleTime" style="width:100%" type="date" :disabled="disableForm" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" :disabled="disableForm" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-col>
      <el-col :span="12">
        <el-row>
          <el-col :span="12">
            <el-form-item label="流程状态" prop="status">
              <el-select v-model="form.status" :disabled="disableForm" style="width: 100%;" placeholder="请选择流程状态">
                <el-option v-for="item in workflowStatus" :key="item.name" :label="item.name" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="编制人" prop="founderName">
              <el-input v-model="form.founderName" :disabled="disableForm" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="编制时间" prop="createTime">
              <el-date-picker v-model="form.createTime" style="width:100%" type="date" :disabled="disableForm" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-col>
      <el-col :span="12">
        <el-row>
          <el-col :span="12">
            <el-form-item label="编制人单位" prop="founderUnit">
              <el-input v-model="form.founderUnit" :disabled="disableForm" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-col>
    </el-row>

    <div style="padding-bottom: 10px;">
      <el-button type="primary" @click="showTargetSetListModal = true">引入设备</el-button>
      <el-button type="primary" @click="deleteDetail">删除设备</el-button>
    </div>
    <com-table
      v-model="selectRow"
      highlight-current-row
      :data="targetList"
      :columns="columns"
      :no-page="true"
    >
      <template v-slot:number="{row}">
        <el-input v-model="row.number" />
      </template>
      <template v-slot:name="{row}">
        <el-input v-model="row.name" />
      </template>
      <template v-slot:specification="{row}">
        <el-input v-model="row.specification" />
      </template>
      <template v-slot:factory="{row}">
        <el-input v-model="row.factory" />
      </template>
      <template v-slot:payTime="{row:{payTime}}">
        {{ dateFormat(payTime) }}
      </template>
      <template v-slot:payValue="{row}">
        <el-input v-model="row.payValue" />
      </template>
      <template v-slot:resetValue="{row}">
        <el-input v-model="row.resetValue" />
      </template>
      <template v-slot:netValue="{row}">
        <el-input v-model="row.netValue" />
      </template>
      <template v-slot:useMonth="{row}">
        <el-input v-model="row.useMonth" />
      </template>
      <template v-slot:handleAmount="{row}">
        <el-input v-model="row.handleAmount" />
      </template>
      <template v-slot:result="{row}">
        <el-input v-model="row.result" />
      </template>
      <template v-slot:remark="{row}">
        <el-input v-model="row.remark" />
      </template>

    </com-table>
    <el-button style="margin-top:10px;" type="primary" @click="showFileModal = true">附件资料({{ fileList.length }})</el-button>
    <file-uploader v-model="fileList" :show-modal.sync="showFileModal" :disabled="disableForm" :can-download="optType==='detail'" />

  </el-form>
</template>

<script>
import { addDeviceScrapHandleInfo, updateDeviceScrapHandleInfo, getDeviceScrapHandleInfoDetail } from '@/api/consumableManage'
// import { getAreaList } from '@/api/common'
import { workflowStatus } from '@/utils/dicData'
import { getConsumableLedgerList } from '@/api/consumableManage'
import fileUploader from '@/components/FileUploader'
import moment from 'moment'
import { turnMap, deviceScrapHand } from '@/views/machineDeviceManage/config.js'
export default {
  name: 'AddDeviceScrapHandleInfo',
  components: {
    fileUploader
  },
  props: {

    optType: {
      type: String,
      default: 'detail'
    },

    data: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      getConsumableLedgerList,
      showFileModal: false,
      selectRow: null,
      targetList: [],
      workflowStatus,

      addType: '', // same   under
      visible: false,
      showAdd: false,
      selectedNode: null,
      form: {
        number: '',
        amount: '',
        handleTime: '',
        remark: '',
        addWorkflowId: '',
        status: '',
        founderName: '',
        createTime: '',
        founderUnit: ''
      },
      columns: [
        {
          label: '设备编号',
          prop: 'number'
        },
        {
          label: '设备名称',
          prop: 'name'
        },
        {
          label: '规格',
          prop: 'specification'
        },
        {
          label: '厂家',
          prop: 'factory'
        },
        {
          label: '购置时间',
          prop: 'payTime',
          slot: 'payTime',
          width: '90'
        },
        {
          label: '购置原值',
          prop: 'payValue'
        },
        {
          label: '重置原值',
          prop: 'resetValue'
        },
        {
          label: '净值',
          prop: 'netValue'
        },
        {
          label: '使用月数',
          prop: 'useMonth'
        },
        {
          label: '处置金额',
          prop: 'handleAmount',
          slot: 'handleAmount'
        },
        {
          label: '处理结果',
          prop: 'result',
          slot: 'result'
        },
        {
          label: '备注',
          prop: 'remark'
        },
        {
          label: '操作',
          prop: 'options',
          slot: 'options'
        }
      ],
      showTargetSetListModal: false,
      fileList: [
      ],
      rules: {
        name: [
          { required: true, message: '单位名称', trigger: 'change' }
        ],
        unifiedSocialCreditCode: [
          { required: true, message: '统一社会信用代码', trigger: 'change' }
        ]
      },
      provinceData: [],
      cityData: []
    }
  },
  computed: {
    disableForm() {
      return this.optType === 'detail'
    }
  },
  watch: {
    data: {
      handler(newData, oldData) {
        if (newData.id) {
          this.getDocDetail(newData.businessId)
        }
      },
      deep: true,
      immediate: true
    }
  },
  mounted() {

  },
  methods: {
    dateFormat(data) {
      if (data) {
        return moment(data).format('YYYY-MM-DD')
      } else {
        return ''
      }
    },
    getSelectedRows(data) {
      this.targetList = this.targetList.concat(data.map(item => {
        return {
          ...item
        }
      }))
    },
    getDocDetail(id) {
      getDeviceScrapHandleInfoDetail({ deviceScrapHandleInfoId: id }).then(res => {
        const { lstAttachment, deviceScrapHandleInfo, lstDeviceScrapHandleDetail } = res.data || {}
        if (lstAttachment && lstAttachment.length > 0) {
          this.fileList = lstAttachment
        }
        Object.keys(this.form).forEach(key => {
          const source = deviceScrapHandleInfo[key]
          if (typeof source === 'boolean') {
            this.form[key] = source ? 1 : 0
          } else {
            this.form[key] = source
          }
        })
        if (lstDeviceScrapHandleDetail && lstDeviceScrapHandleDetail.length > 0) {
          this.targetList = lstDeviceScrapHandleDetail
        }
      })
    },
    deleteDetail() {
    },
    // 数据处理
    dealData(obj) {
      const tmpObj = {}
      for (const key in deviceScrapHand) {
        const val = turnMap[key]
        tmpObj[key] = obj[key] ? obj[key] : obj[val]
      }
      return tmpObj
    },
    saveDoc() {
      if (this.optType === 'detai') {
        this.show = false
        return
      }
      this.$refs.form.validate((valid) => {
        if (valid) {
          const tmpArr = []
          this.targetList.map(item => {
            const tmpObj = this.dealData(item)
            tmpArr.push(tmpObj)
          })
          const params = {
            deviceScrapHandleInfo: {
              ...this.form
            },
            lstAttachment: this.fileList,
            lstDeviceScrapHandleDetail: tmpArr
          }
          const fn = this.optType === 'add' ? addDeviceScrapHandleInfo : updateDeviceScrapHandleInfo
          fn(params).then(res => {
            if (res.code === '0001') {
              this.$message({
                type: 'success',
                message: '保存成功!'
              })
              this.show = false
              this.$emit('freshList')
              this.$refs.form.resetFields()
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.com-form-title{
  font-size: 14px;
  font-weight: bold;
  line-height: 35px;
  color: #409EFF;
  border-bottom: 1px solid #dedede;
  margin-bottom: 6px;
}

::v-deep .el-form-item__label{
  color: #000;
}
::v-deep .el-input.is-disabled .el-input__inner{
  color: #000;
}
::v-deep .el-input.is-disabled .el-input__inner{
  color: #000;

}
</style>
